package com.gack.business.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.gack.business.dao.StoreBookingDetail;
import com.gack.business.dao.StoreBookingDetailDao;
import com.gack.business.dao.StoreFacityAndServiceDao;
import com.gack.business.dao.StoreRevenuenobind;
import com.gack.business.dao.StoreUseInfoDao;
import com.gack.business.dao.StoreUseInfonobind;
import com.gack.business.dao.StoresDao;
import com.gack.business.model.BackUser;
import com.gack.business.model.StoreAndFacity;
import com.gack.business.model.StoreAndService;
import com.gack.business.model.StoreUniteInfo;
import com.gack.business.model.VideoStores;
import com.gack.business.repository.BackUserRepository;
import com.gack.business.repository.StoreAndFacityRepository;
import com.gack.business.repository.StoreAndServiceRepository;
import com.gack.business.repository.StoreSupportFacityRepository;
import com.gack.business.repository.StoreSupportServiceRepository;
import com.gack.business.repository.StoreUniteInfoRepository;
import com.gack.business.repository.VideoStoresRepository;
import com.gack.business.vo.StoresVO;
import com.gack.helper.common.abstractobj.Result;

/**
 * 
* @ClassName: StoresOptService 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author (ZhangXinYu)  
* @date 2018年6月4日 下午3:09:54 
* @version V1.0
 */
@Service
public class StoresService implements StoresServiceInterface{

	@Autowired
	private VideoStoresRepository storesRepository;
	@Autowired
	private StoresDao storesDao;
	@Autowired
	private StoreSupportFacityRepository facityRepository;
	@Autowired
	private StoreSupportServiceRepository serviceRepository;
	@Autowired
	private StoreFacityAndServiceDao facityAndServiceDao;
	@Autowired
	private StoreAndFacityRepository storeAndFacityRepository;
	@Autowired
	private StoreAndServiceRepository storeAndServiceRepository;
	@Autowired
	private StoreUniteInfoRepository storeUniteInfoRepository;
	@Autowired
	private StoreUseInfoDao storeUseInfoDao;
	@Autowired
	private StoreBookingDetailDao storeBookingDetailDao;
	@Autowired
	private BackLogServiceInterface backLogService;
	@Autowired
	private BackUserRepository backUserRepository;
	@Autowired
	private JPushServer jPushServer;
	
	
	/**
	 * 查询门店信息
	 * searchType  查询类型  0无条件查询  1 门店名称查询  2门店面积查询  3门店使用价格查询
	 */
	@Override
	public Map<String, Object> getStores(String storeid,Integer searchType, String searchKey, Integer currentPage, Integer pageSize) {
		Map<String , Object> map = new HashMap<>();
		Sort sort = new Sort(Sort.Direction.DESC, "createtime");
		Pageable pageable = new PageRequest(currentPage, pageSize, sort);
		Page<VideoStores> stores = null;
		if(storeid == null){
			if(searchType == 0){
				stores = storesRepository.findByIsUseNot(1,pageable);
			}else if(searchType == 1){
				stores = storesRepository.findByStoreNameLikeAndIsUseNot("%"+searchKey+"%", 1,pageable);
			}else if(searchType == 2){
				stores = storesRepository.findByStoreAreaAndIsUseNot(Integer.valueOf(searchKey),1, pageable);
			}else if(searchType == 3){
				stores = storesRepository.findByStorePriceAndIsUseNot(Integer.valueOf(searchKey),1, pageable);
			}
		}else{
			stores = storesRepository.findById(storeid, pageable);
		}
//		Integer deposit = storesRepository.findMaxDeposit();
		Integer deposit = 0;
		StoreUniteInfo storeUniteInfo = storeUniteInfoRepository.findUniteInfoByKey("deposit");
		if(storeUniteInfo != null){
			String deposit_str = storeUniteInfo.getUniteValue(); 
			if(deposit_str != null && !deposit_str.trim().equals("")){
				try{
					deposit = Integer.valueOf(deposit_str.trim());
				}catch (NumberFormatException e) {
					e.printStackTrace();
//					deposit = storesRepository.findMaxDeposit();
				}
			}
		}
		map.put("key", "success");
		map.put("stores", stores);
		map.put("deposit", deposit);
		return map;
	}

	/**
	 * 非条件查询门店信息
	 */
	@Override
	public Map<String, Object> getAllStores(Integer currentPage, Integer pageSize) {
		Map<String , Object> map = new HashMap<>();
		Sort sort = new Sort(Sort.Direction.DESC,"createtime");
		Pageable pageable = new PageRequest(currentPage, pageSize,sort);
		Page<VideoStores> stores = null;
		stores = storesRepository.findByIsUseNot(1,pageable);
		Integer deposit = storesRepository.findMaxDeposit();
		map.put("key", "success");
		map.put("stores", stores);
		map.put("deposit", deposit);
		return map;
	}
	
	@Override
	public List<VideoStores> getAllStores(){
//		return storesRepository.findAll();
		return storesRepository.findAllByIsUseNot(1);
	}
	
	@Override
	public List<VideoStores> getAllNoUserStores(String userid){
		List<VideoStores> stores = null;
		stores = storesRepository.findAllNoUserStores();
		//userid 不为空  用户存在  且存在管理门店   查询管理门店  若可以查询到管理门店  管理门店未被删除 则添加管理门店到门店列表中
		if(userid != null && !userid.equals("")){
			BackUser user = backUserRepository.findOne(userid);
			if(user != null && user.getStoreid() != null && !user.getStoreid().equals("")){
				VideoStores store = storesRepository.findOne(user.getStoreid());
				//门店存在   且  门店未被删除  将门店添加到门店列表当中
				if(store != null && store.getIsUse() != null && store.getIsUse() != 1){
					stores.add(store);
				}
			}
		}
		return stores;
	}

	/**
	 * 门店细信息导出
	 */
	@Override
	public Workbook exportStores() {
		Workbook wb = new HSSFWorkbook();
		Sheet sheet = wb.createSheet("门店信息");
		Row header = sheet.createRow(0);
		header.createCell(0).setCellValue("编号");
		header.createCell(1).setCellValue("门店名称");
		header.createCell(2).setCellValue("门店地址");
		header.createCell(3).setCellValue("门店开始营业时间");
		header.createCell(4).setCellValue("门店结束营业时间");
		header.createCell(5).setCellValue("门店配套设置");
		header.createCell(6).setCellValue("门店配套服务");
		header.createCell(7).setCellValue("门店管理者");
		header.createCell(8).setCellValue("门店管理者联系方式");
		header.createCell(9).setCellValue("门店座机号码");
		header.createCell(10).setCellValue("注意事项");
		header.createCell(11).setCellValue("门店经度");
		header.createCell(12).setCellValue("门店维度");
		header.createCell(13).setCellValue("门店首页图片");
		header.createCell(14).setCellValue("门店第一张图片");
		header.createCell(15).setCellValue("门店第二张图片");
		header.createCell(16).setCellValue("门店第三张图片");
		header.createCell(17).setCellValue("门店第四张图片");
		header.createCell(18).setCellValue("门店第五张图片");
		header.createCell(19).setCellValue("门店第六张图片");
		header.createCell(20).setCellValue("门店面积");
		header.createCell(21).setCellValue("门店押金");
		header.createCell(22).setCellValue("门店价格");
		header.createCell(23).setCellValue("门店状态");
		List<VideoStores> stores = storesRepository.findAll();
		for(int i=0;i<stores.size();i++){
			VideoStores store = stores.get(i);
			Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
			dataRow.createCell(0).setCellValue(store.getId()!=null?store.getId():"");
			dataRow.createCell(1).setCellValue(store.getStoreName()!=null?store.getStoreName():"");
			dataRow.createCell(2).setCellValue(store.getStoreAddress()!=null?store.getStoreAddress():"");
			Date startDate = store.getStarttime();
			Date endDate = store.getEndtime();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm:ss");
			dataRow.createCell(3).setCellValue(startDate!=null?simpleDateFormat.format(startDate):"");
			dataRow.createCell(4).setCellValue(endDate!=null?simpleDateFormat.format(endDate):"");
			dataRow.createCell(5).setCellValue(store.getSupportfacity()!=null?store.getSupportfacity():"");
			dataRow.createCell(6).setCellValue(store.getStoreService()!=null?store.getStoreService():"");
			dataRow.createCell(7).setCellValue(store.getStoreUser()!=null?store.getStoreUser():"");
			dataRow.createCell(8).setCellValue(store.getStorePhone()!=null?store.getStorePhone():"");
			dataRow.createCell(9).setCellValue(store.getStoreLandline()!=null?store.getStoreLandline():"");
			dataRow.createCell(10).setCellValue(store.getAttention()!=null?store.getAttention():"");
			dataRow.createCell(11).setCellValue(store.getLongitude()!=null?store.getLongitude():"");
			dataRow.createCell(12).setCellValue(store.getLatitude()!=null?store.getLatitude():"");
			dataRow.createCell(13).setCellValue(store.getStorep_hotourl()!=null?store.getStorep_hotourl():"");
			dataRow.createCell(14).setCellValue(store.getOnephotourl()!=null?store.getOnephotourl():"");
			dataRow.createCell(15).setCellValue(store.getTwophoneurl()!=null?store.getTwophoneurl():"");
			dataRow.createCell(16).setCellValue(store.getThreephotourl()!=null?store.getThreephotourl():"");
			dataRow.createCell(17).setCellValue(store.getFourphotourl()!=null?store.getFourphotourl():"");
			dataRow.createCell(18).setCellValue(store.getFivephotourl()!=null?store.getFivephotourl():"");
			dataRow.createCell(19).setCellValue(store.getSixphotourl()!=null?store.getSixphotourl():"");
			dataRow.createCell(20).setCellValue(store.getStoreArea());
			dataRow.createCell(21).setCellValue(store.getStoreDeposit());
			dataRow.createCell(22).setCellValue(store.getStorePrice());
			String status = "";
			if(store.getStatus() == 0){
				status = "可使用";
			}else if(store.getStatus() == 1){
				status = "预约中";
			}else if(store.getStatus() == 2){
				status = "使用中";
			}
			dataRow.createCell(23).setCellValue(status);
		}
		return wb;
	}

	/**
	 *  获取门店详情
	 */
	@Override
	public Result getStoresInfo(String storeid) {
		Result result = new Result();
		VideoStores stores = storesRepository.findOne(storeid);
		if(stores == null){
			result.setKey("error_noneStores");
			result.setMsg("门店不存在，门店详情查询失败");
			return result;
		}
		result.setKey("success");
		result.setValue(stores);
		return result;
	}

	/**
	 * 添加新门店
	 * String storeName, Integer storeDeposit, Integer storeArea, List<String> supportFacities,
			List<String> storeService, Date startTime, Date endTime, String storeAddress, String longitude, String latitude,
			Integer storePrice, String storeUser, String storePhone, String storeLandline, String storep_hotourl,
			String onephotourl, String twophoneurl, String threephotourl, String fourphotourl, String fivephotourl,
			String sixphotourl
	 */
	@Override
	public Result addNewStore(String userid,String storeName,Integer storeDeposit,Integer storeArea,
			String supportFacities,String storeService,String startTime,String endTime,String storeAddress,
			String longitude,String latitude,Integer storePrice,String storeUser,String storePhone,
			String storeLandline,String storep_hotourl,String onephotourl,String twophoneurl,String threephotourl,
			String fourphotourl,String fivephotourl,String sixphotourl,String province,String city,String area) {
		Result result = new Result();
		List<String> sfList = new ArrayList<>();
		if(supportFacities != null){
			sfList = new ArrayList<>(Arrays.asList(supportFacities.split("@;-;@")));
		}
		List<String> ssList = new ArrayList<>();
		if(storeService != null){
			ssList = new ArrayList<>(Arrays.asList(storeService.split("@;-;@")));
		}
		Integer count = storesRepository.getTheSameCountOfStoreName(storeName);
		if(count > 0){
			result.setKey("error_duplicateName");
			result.setMsg("门店地点不可重复添加");
			return result;
		}
		VideoStores stores = new VideoStores();
		stores.setStoreName(storeName);
		stores.setStoreDeposit(storeDeposit);
		stores.setStoreArea(storeArea);
		stores.setSupportfacity(facityAndServiceDao.getFacityNamesByIds(sfList));
		stores.setStoreService(facityAndServiceDao.getServiceNamesByIds(ssList));
		//获取配套设施列表
//		List<StoreSupportFacity> facities = facityRepository.getFacityByIds(newStoreVO.getSupportFacities());
		//获取配套服务列表
//		List<StoreSupportService> services = serviceRepository.getServiceByIds(newStoreVO.getStoreService());
		//格式化日期
		Date start = null;
		Date end = null;
		if(startTime != null && !startTime.equals("")){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
			try{
				Date d = format.parse(startTime.replace("Z", " UTC"));
//				format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//				startTime = format.format(d);
				start = d;
//				System.out.println(start);
//				System.out.println(startTime);
			} catch (ParseException e) {
				e.printStackTrace();
			}//注意是空格+UTC
		}
		if(endTime != null && !endTime.equals("")){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
			try{
				Date d = format.parse(endTime.replace("Z", " UTC"));
//				format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//				endTime = format.format(d);
				end = d;
			} catch (ParseException e) {
				e.printStackTrace();
			}//注意是空格+UTC
		}
		stores.setStarttime(start);
		stores.setEndtime(end);
		stores.setStoreAddress(storeAddress);
		stores.setLongitude(longitude);
		stores.setLatitude(latitude);
		stores.setStorePrice(storePrice);
//		stores.setStoreUser(storeUser);
//		stores.setStorePhone(storePhone);
		stores.setStoreLandline(storeLandline);
		stores.setStorep_hotourl(storep_hotourl);
		stores.setOnephotourl(onephotourl);
		stores.setTwophoneurl(twophoneurl);
		stores.setThreephotourl(threephotourl);
		stores.setFourphotourl(fourphotourl);
		stores.setFivephotourl(fivephotourl);
		stores.setSixphotourl(sixphotourl);
		stores.setCreatetime(new Date());
		stores.setStatus(1);
		stores.setIsUse(2);		//默认门店不营业
		stores.setProvince(province);
		stores.setCity(city);
		stores.setArea(area);
		stores = storesRepository.save(stores);
		//添加门店与配套设施，配套服务关系
		String storeid = stores.getId();
		if(sfList != null && sfList.size() > 0){
			List<StoreAndFacity> storeAndFacities = new ArrayList<>();
			for(String facityId : sfList){
				StoreAndFacity saf = new StoreAndFacity();
				saf.setStoreid(storeid);
				saf.setFacityid(facityId);
				storeAndFacities.add(saf);
			}
			storeAndFacityRepository.save(storeAndFacities);
			//刷新门店配套设施使用状态
			facityAndServiceDao.updateStoreSupportFacityIsUse();
		}
		if(ssList != null && ssList.size() > 0){
			List<StoreAndService> storeAndServices = new ArrayList<>();
			for(String serviceId : ssList){
				StoreAndService sas = new StoreAndService();
				sas.setStoreid(storeid);
				sas.setServiceid(serviceId);
				storeAndServices.add(sas);
			}
			storeAndServiceRepository.save(storeAndServices);
			//刷新门店配套服务使用状态
			facityAndServiceDao.updateStoreSupportServiceIsUse();
		}
		
		// 门店状态修改 推送提示刷新
		jPushServer.sendPush("", "22", "", null);
		
		backLogService.addBackLog(userid, "增加新门店>> 新增门店id："+stores.getId());
		result.setKey("success");
		result.setValue(stores);
		return result;
	}

	/**
	 * 门店修改时查询门店详情
	 */
	@Override
	public Result getEditStoresInfo(String storeid) {
		Result result = new Result();
		VideoStores stores = storesRepository.findOne(storeid);
		if(stores == null){
			result.setKey("error_notExists");
			result.setMsg("门店不存在");
			return result;
		}
		if(stores.getIsUse() == 1){
			result.setKey("error_notExists");
			result.setMsg("门店已被删除");
			return result;
		}
		StoresVO storesVO = storesDao.getStoreInfo(storeid);
		if(storesVO == null){
			result.setKey("error_noneStore");
			result.setMsg("门店不存在，门店详情查询失败");
			return result;
		}
		result.setKey("success");
		result.setValue(storesVO);
		return result;
	}

	/**
	 * 删除门店
	 */
	@Override
	public Result dealOneStore(String userid,String storeid) {
		Result result = new Result();
		//删除门店   删除门店记录  删除门店相关配套服务和配套设施关联
		//删除门店为假删除   保留门店记录   修改门店可用状态
		VideoStores stores= storesRepository.getOne(storeid);
		if(stores == null){
			result.setKey("error_noneStore");
			result.setMsg("门店不存在，删除门店操作失败");
			return result;
		}
		long count = storesDao.getUnEndUsedStoreOrderCount(storeid);
		if(count > 0 ){
			result.setKey("error_notEnd");
			result.setMsg("门店存在使用中订单,删除门店操作失败");
			return result;
		}
//		storeAndFacityRepository.dealStoreFacitiesByStoreid(storeid);
//		storeAndServiceRepository.dealStoreServiceByStoreid(storeid);
//		storesRepository.delete(stores);
		stores.setIsUse(1);
		stores = storesRepository.save(stores);
		facityAndServiceDao.dealStoreSupportFacityAndServiceRelation(stores.getId());
		facityAndServiceDao.updateStoreSupportFacityIsUse();
		facityAndServiceDao.updateStoreSupportServiceIsUse();
		
		// 门店状态修改 推送提示刷新
		jPushServer.sendPush("", "22", "", null);
		
		backLogService.addBackLog(userid, "删除门店>> 门店id："+stores.getId());
		result.setKey("success");
		result.setValue(null);
		result.setMsg("门店删除成功");
		return result;
	}

	/**
	 * 负责人修改门店
	 * private String id;	//门店id
	private List<String> supportFacities;	//门店配套设施ids
	private List<String> storeService;		//门店配套服务ids
	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
	@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
	private Date startTime;				//开始时间
	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
	@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
	private Date endTime;				//结束时间
	private Integer storePrice;			//使用价格
	private String storePhone;			//联系人电话
	private String storeLandline;		//门店座机
	private String storep_hotourl;		//首页图
	private String onephotourl;			//第一张图片url
	private String twophoneurl;			//第二张图片url
	private String threephotourl;		//第三张图片url
	private String fourphotourl;		//第四张图片url
	private String fivephotourl;		//第五张图片url
	private String sixphotourl;			//第六张图片url
	 */
	@Override
	public Result updateStoreInfo(String userid,String id,String supportFacities,String storeService,String startTime,
			String endTime,Integer storePrice,String storePhone,String storeLandline,String storep_hotourl,String onephotourl,
			String twophoneurl,String threephotourl,String fourphotourl,String fivephotourl,String sixphotourl,Integer isuse,
			String storeUser,Integer storeArea, String storeName) {
		Result result = new Result();
		VideoStores stores = storesRepository.findOne(id);
		if(stores == null){
			result.setKey("error_notExists");
			result.setMsg("门店不存在");
			return result;
		}
		if(stores.getIsUse() == 1){
			result.setKey("error_notExists");
			result.setMsg("门店已被删除");
			return result;
		}
		if(isuse != null && isuse == 2 && storesDao.getUsedStoreOrderCount(stores.getId()) > 0){
			result.setKey("error_isUsed");
			result.setMsg("门店正在被预约/使用中，无法关闭门店");
			return result;
		}
		List<String> sfList = new ArrayList<>();
		if(supportFacities != null){
			sfList = new ArrayList<>(Arrays.asList(supportFacities.split("@;-;@")));
		}
		List<String> ssList = new ArrayList<>();
		if(storeService != null){
			ssList = new ArrayList<>(Arrays.asList(storeService.split("@;-;@")));
		}
		VideoStores store = null;
		store = storesRepository.getOne(id);
		if(store == null){
			result.setKey("error_noneStore");
			result.setMsg("门店不存在，修改门店失败");
			return result;
		}
		if(storesDao.hasName(id, storeName)){
			result.setKey("error_duplicateName");
			result.setMsg("门店名称已存在");
			return result;
		}
		store.setSupportfacity(facityAndServiceDao.getFacityNamesByIds(sfList));
		store.setStoreService(facityAndServiceDao.getServiceNamesByIds(ssList));
		//格式化日期
		Date start = null;
		Date end = null;
		if(startTime != null && !startTime.equals("")){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
			try{
				Date d = format.parse(startTime.replace("Z", " UTC"));
//						format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//						startTime = format.format(d);
				start = d;
			} catch (ParseException e) {
				e.printStackTrace();
			}//注意是空格+UTC
		}
		if(endTime != null && !endTime.equals("")){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
			try{
				Date d = format.parse(endTime.replace("Z", " UTC"));
//						format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//						endTime = format.format(d);
				end = d;
			} catch (ParseException e) {
				e.printStackTrace();
			}//注意是空格+UTC
		}
		store.setStarttime(start);
		store.setEndtime(end);
		store.setStorePrice(storePrice);
//		store.setStoreUser(storeUser);
//		store.setStorePhone(storePhone);
		store.setStoreLandline(storeLandline);
		if(storep_hotourl != null && !storep_hotourl.equals("")){
			store.setStorep_hotourl(storep_hotourl);
		}
		store.setOnephotourl(onephotourl);
		store.setTwophoneurl(twophoneurl);
		store.setThreephotourl(threephotourl);
		store.setFourphotourl(fourphotourl);
		store.setFivephotourl(fivephotourl);
		store.setSixphotourl(sixphotourl);
		store.setIsUse(isuse);
		store.setStoreArea(storeArea);
		if(storeName != null && !storeName.equals("")){
			store.setStoreName(storeName);
		}
		store = storesRepository.save(store);
		//更新门店与配套设施，配套服务关系
		String storeid = store.getId();
		if(sfList != null && sfList.size() > 0){
			storeAndFacityRepository.dealStoreFacitiesByStoreid(storeid);
			List<StoreAndFacity> storeAndFacities = new ArrayList<>();
			for(String facityId : sfList){
				StoreAndFacity saf = new StoreAndFacity();
				saf.setStoreid(storeid);
				saf.setFacityid(facityId);
				storeAndFacities.add(saf);
			}
			storeAndFacityRepository.save(storeAndFacities);
			//更新门店配套设施使用状态
			facityAndServiceDao.updateStoreSupportFacityIsUse();
		}
		if(ssList != null && ssList.size() > 0){
			storeAndServiceRepository.dealStoreServiceByStoreid(storeid);
			List<StoreAndService> storeAndServices = new ArrayList<>();
			for(String serviceId : ssList){
				StoreAndService sas = new StoreAndService();
				sas.setStoreid(storeid);
				sas.setServiceid(serviceId);
				storeAndServices.add(sas);
			}
			storeAndServiceRepository.save(storeAndServices);
			//更新门店配套服务使用状态
			facityAndServiceDao.updateStoreSupportServiceIsUse();
		}
		
		// 门店状态修改 推送提示刷新
		jPushServer.sendPush("", "22", "", null);
		
		backLogService.addBackLog(userid, "负责人修改门店>> 门店id："+store.getId());
		result.setKey("success");
		result.setValue(store);
		return result;
	}

	/**
	 * 获取门店使用详情
	 */
	@Override
	public Result getStoreUseInfo(String storeid,String orderid) {
		Result result = new Result();
		if(storeid == null || storeid.equals("")){
			result.setKey("error");
			result.setMsg("门店id不可为空");
			return result;
		}
		VideoStores stores = storesRepository.findOne(storeid);
		if(stores == null){
			result.setKey("error_notExists");
			result.setMsg("门店不存在");
			return result;
		}
		if(stores.getIsUse() == 1){
			result.setKey("error_notExists");
			result.setMsg("门店已被删除");
			return result;
		}
		if(orderid == null || orderid.equals("")){
			result.setKey("error");
			result.setMsg("订单id错误，无法查询");
			return result;
		}
		StoreUseInfonobind storeUseInfonobind = null;
		storeUseInfonobind = storeUseInfoDao.getStoreUseInfo(orderid.trim());
		result.setKey("success");
		result.setValue(storeUseInfonobind);
		return result;
	}

	@Override
	public Result getStoreBookingDetail(String storeid,String orderid) {
		Result result = new Result();
		if(storeid == null || storeid.equals("")){
			result.setKey("error");
			result.setMsg("门店id不可为空");
			return result;
		}
		VideoStores stores = storesRepository.findOne(storeid);
		if(stores == null){
			result.setKey("error_notExists");
			result.setMsg("门店不存在");
			return result;
		}
		if(stores.getIsUse() == 1){
			result.setKey("error_notExists");
			result.setMsg("门店已被删除");
			return result;
		}
		if(orderid == null || orderid.equals("")){
			result.setKey("error");
			result.setMsg("订单id错误，无法查询");
			return result;
		}
		StoreBookingDetail storeBookingDetail = null;
		storeBookingDetail = storeBookingDetailDao.getStoreBookingDetail(orderid.trim());
		result.setKey("success");
		result.setValue(storeBookingDetail);
		return result;
	}

	/**
	 * 获取门店预约列表
	 */
	@Override
	public Map<String, Object> getStoreReservationList(String storeid,String beginTime, String endTime, Integer page, Integer size) {
		Map<String, Object> map = new HashMap<>();
		if(storeid == null || storeid.equals("")){
			map.put("key", "error");
			map.put("msg", "门店id不可为空");
			return map;
		}
		VideoStores stores = storesRepository.findOne(storeid);
		if(stores == null){
			map.put("key", "error_notExists");
			map.put("msg", "门店不存在");
			return map;
		}
		if(stores.getIsUse() == 1){
			map.put("key", "error_notExists");
			map.put("msg", "门店已被删除");
			return map;
		}
		List<StoreBookingDetail> storeReservations = storeBookingDetailDao.getStoreReservationList(storeid,beginTime, endTime, page, size);
		long total = storeBookingDetailDao.getStoreReservationTotal(storeid,beginTime, endTime, page, size);
		map.put("key", "success");
		map.put("stores", storeReservations);
		map.put("total", total);
		return map;
	}

	/**
	 * 获取门店使用列表
	 */
	@Override
	public Map<String, Object> getStoreUseInfoList(String storeid,String beginTime, String endTime, Integer page, Integer size) {
		Map<String, Object> map = new HashMap<>();
		if(storeid == null || storeid.equals("")){
			map.put("key", "error");
			map.put("msg", "门店id不可为空");
			return map;
		}
		VideoStores stores = storesRepository.findOne(storeid);
		if(stores == null){
			map.put("key", "error_notExists");
			map.put("msg", "门店不存在");
			return map;
		}
		if(stores.getIsUse() == 1){
			map.put("key", "error_notExists");
			map.put("msg", "门店已被删除");
			return map;
		}
		List<StoreUseInfonobind> storeUseInfonobinds = storeUseInfoDao.getStoreUseInfoList(storeid,beginTime, endTime, page, size);
		long total = storeUseInfoDao.getStoreUseInfoTotal(storeid,beginTime, endTime, page, size);
		map.put("key", "success");
		map.put("stores", storeUseInfonobinds);
		map.put("total", total);
		return map;
	}

	/**
	 * 查询门店收入情况
	 */
	@Override
	public Map<String, Object> getStoreRevenue(Integer type,String province ,String city,String area,String beginTime, String endTime,Integer page,Integer size) {
		Map<String, Object> map = new HashMap<>();
		List<StoreRevenuenobind> stores = storesDao.getStoreRevenues(type,province,city,area,beginTime, endTime,page,size);
		long total = storesDao.getTotalStoreRevenue(type,province,city,area,beginTime, endTime);
		map.put("key", "success");
		map.put("stores", stores);
		map.put("total", total);
		return map;
	}
	
	
	
	
}
